<?php

defined('SYSPATH') or die('Access error!');

class Model_Shop_Order extends Model
{

    /**
     *  得到订单的列表（带分页）
     * @param type $where_sql
     * @param type $nums
     * @return type
     */
    public function get_order_list($where_sql = ' ', $nums = 10)
    {
        if ($where_sql != '')
        {
            $where_sql = ' WHERE ' . $this->get_where($where_sql);
        }
        $where_sql .= " ORDER BY id DESC ";
        $sql = 'SELECT id,buyer_name,buyer_userid,order_sn,add_time,status,seller_name,seller_userid ' .
                'FROM ' . $this->table . $where_sql;
        $data = $this->get_page_list($sql, $nums);
        return $data;
    }

    public function get_order_detail($id)
    {
        $order_info = $this->get_row(array('id' => $id));
        if (Arr::validate_arr($order_info))
        {
            $order_info['goods_list'] = $this->get_order_goods($order_info);
            $order_info['order_logs'] = $this->get_order_log($order_info['id']);
            $order_info['account_str'] = Model::factory('shop_goods')->get_goods_account($order_info['order_account'], $order_info['account_type']);
        }
        return $order_info;
    }

    /**
     *  根据订单的id获取订单的产品
     * @param type $order_id
     */
    public function get_order_goods($order_info)
    {
        $goods_list_temp = Model::factory('shop_order_goods')->get_results(' and order_id=' . $order_info['id']);
        $goods_list = array();
        if (Arr::validate_arr($goods_list_temp))
        {
            foreach ($goods_list_temp as $key => $value)
            {
                $value['goods_url'] = Model::factory('shop_goods')->create_goods_url($value['goods_id']);
                $value['is_comment'] = 'uncomment';
                if (Model::factory('shop_goods_comments')->is_exist(array('goods_id' => $value['goods_id'], 'user_id' => Session::instance()->get_detail('user', 'id'))))
                {
                    $value['is_comment'] = 'commented';
                }

                $value['is_reply'] = 'unreply';
                if (Model::factory('shop_goods_comments')->is_exist(array('goods_id' => $value['goods_id'], 'reply_content!' => '','user_id'=>$order_info['buyer_userid'])))
                {
                    $value['is_reply'] = 'replyed';
                }
                
                $value['comment_id'] = Model::factory('shop_goods_comments')->get_var(array('user_id'=>$order_info['buyer_userid'],'goods_id' => $value['goods_id']),'id');
                $goods_list[] = $value;
            }
        }

        return $goods_list;
    }

    /**
     *  根据订单号获取订单的日志
     * @param type $order_id
     */
    public function get_order_log($order_id)
    {
        return Model::factory('shop_order_log')->get_results(' and order_id=' . $order_id);
        ;
    }

    /**
     *  获取产品的订单
     * @param type $goods_id
     * @return type
     */
    public function get_goods_order($goods_id)
    {
        $sql = "SELECT o.* " .
                "FROM " . $this->table . " AS o " .
                "LEFT JOIN " . $this->db->get_table('shop_order_goods') . " AS og ON og.order_id=o.id " .
                "WHERE o.status='success' AND og.goods_id=" . $goods_id;
        $goods_order_list = $this->db->get_results($sql, ARRAY_A);
        return $goods_order_list;
    }
    

    

}